const Router = require('koa-router')
const bcrypt = require('bcryptjs')

//采用全局的方式替换单个导入
const {HttpException, ParameterException} = require('../../../core/http-exception')
const {PositiveIntegerValidator} = require('../../validators/validator')
const {ClassicValidator} = require('../../validators/classic')
const {Auth} = require('../../../middlewares/auth')


const {Movie, Music, Sentence} = require('@models/classicModel')
const {Flow} = require('@models/flowModel')
const {Art} = require('../../model/ArtModel')
const { Favor } = require('../../model/favorModel')

const router = new Router({
  prefix: '/v1/classic'
  
})
router.get('/latest',new Auth(9).m, async (ctx, next) => {
  // 权限 复杂
  // 限制 token 角色
  // 普通用户 管理员
  // 分级 scope
  // 8 16
  const flow = await Flow.findOne({
    order: [
      ['index', 'DESC'] // 倒序排
    ]
  })
  // const flow2 = await Flow.findAll({
  //   order: [
  //     ['index', 'DESC'] // 倒序排
  //   ]
  // })
  console.log(flow)
  const art = await Art.getData(flow.art_id, flow.type)
  // js动态，私有成员 
  // flow.dataValues.index = flow.index
  art.setDataValue('index', flow.index)
  
  // // ctx.body = ctx.auth
  ctx.body = art;
  // 序列化对象
  // js序列
}) 

// 获取报刊点赞数量2
router.get('/:type/:id', new Auth().m, async ctx => {
  const v = await new ClassicValidator().validate(ctx)
  const id = v.get('path.id')
  const type = parseInt(v.get('path.type'))
  
  const artDetail = await new Art(id, type).getDetail(ctx.auth.uid);
  if(!artDetail) {
    throw new global.errs.NotFound()
  } 

  // const like = await Favor.userLikeIt(
  //   id, type, ctx.auth.uid
  // )
  ctx.body = {
      art: artDetail.art,
      like_status: artDetail.like_status
  }
})


// 获取报刊点赞数量
router.get('/:type/:id/favor', new Auth().m, async ctx => {
  const v = await new ClassicValidator().validate(ctx)
  const id = v.get('path.id')
  const type = parseInt(v.get('path.type'))
  
  const art = await Art.getData(id, type);
  if(!art) {
    throw new global.errs.NotFound()
  } 

  const like = await Favor.userLikeIt(
    id, type, ctx.auth.uid
  )
  ctx.body = {
      fav_nums: art.fav_nums,
      like_status: like
  }
})

router.get('/favors', new Auth().m, async ctx=> {
  const favors = await Favor.getMyClassicFavors(ctx.auth.uid)
  if(favors) {
    ctx.body = {
      list: favors
    }
  }

})

// router.get('/v1/classic', (ctx, next)=> {
//   ctx.body = {
//     key: 'hello:'+ ctx.path,
//     fresh: ctx.request.fresh,
//     header: ctx.request.header,
//     requestSecure: ctx.request.secure,
//     requestProtocol: ctx.request.protocol,
//     requestIp: ctx.request.ip
//   }
// })

// router.post('/v1/classic', (ctx, next)=> {
//   ctx.body = {
//     key: 'hello:'+ ctx.path,
//     fresh: ctx.request.fresh
//   }
// })

// router.post('/v1/:id/classic', (ctx, next)=> {

//   ctx.body = {
//     key: 'hello:'+ ctx.path,
//     path: ctx.params,
//     query: ctx.request.query,
//     headers: ctx.request.header,
//     body: ctx.request.body

//   } 
//   // const v = new PositiveIntegerValidator().validate(ctx)

// })

// router.post('/v1/exception', (ctx,next)=> {
//   throw new Error('出现异常了捕获吗')
// })

// router.post('/v1/exception/restful', (ctx,next)=> {
//   // 定义一个类 ，面向对象 多行注释 option+ shift + A
//  /*  const err = new Error('参数异常了呀')
//   err.status = 400
//   err.errorCode = 10001 */
//   // const err = new HttpException('消除模仿', 10001, 400)
//   const err = new ParameterException()


//   throw err;
// })

module.exports = router
